package mredis

import (
	"strings"
	"time"
	"fmt"
	"errors"
)

//Redis Bgrewriteaof 命令用于异步执行一个 AOF（AppendOnly File） 文件重写操作。重写会创建一个当前 AOF 文件的体积优化版本。
func (c *Command) BgRewriteAof () (string,error) {
	if c.conn == nil {
		return "",RedisConnError
	}
	_,err := c.conn.WriteString("BGREWRITEAOF\r\n")
	if err != nil {
		return "",err
	}
	b,err := c.conn.ReadAsString()
	if err != nil {
		return "",err
	}
	return b,nil
}
//Redis Bgsave 命令用于在后台异步保存当前数据库的数据到磁盘。
func (c *Command) BgSave()(string,error) {
	if c.conn == nil {
		return "",RedisConnError
	}
	_,err := c.conn.WriteString("BGSAVE\r\n")
	if err != nil {
		return "",err
	}
	b,err := c.conn.ReadAsString()
	if err != nil {
		return "",err
	}
	return b,nil
}
//Redis Client Kill 命令用于关闭客户端连接。
func (c *Command) ClientKill(ip string,port int)(error){
	if c.conn == nil {
		return RedisConnError
	}
	_,err := c.conn.WriteStringFormat("CLIENT KILL %s:%d\r\n",ip,port)
	if err != nil {
		return err
	}
	b,err := c.conn.ReadAsString()
	if err != nil {
		return err
	}
	if b != "OK" {
		return RedisError("Unexpected response to CLIENT KILL:" + b)
	}
	return nil
}
//Redis Client List 命令用于返回所有连接到服务器的客户端信息和统计数据。
func (c *Command) ClientList() ([]string,error)  {
	if c.conn == nil {
		return nil,RedisConnError
	}
	_,err := c.conn.WriteString("CLIENT LIST\r\n")
	if err != nil {
		return nil,err
	}

	b,err := c.conn.ReadAsString()
	if err != nil {
		return nil,err
	}
	return strings.Split(b,"\n"),nil
}
//获取连接的名称
func (c *Command) GetName() (string,error)  {
	if c.conn == nil {
		return "",RedisConnError
	}
	_,err := c.conn.WriteString("CLIENT GETNAME\r\n")
	if err != nil {
		return "",err
	}

	b,err := c.conn.ReadAsString()
	if err != nil {
		return "",err
	}
	return b,nil
}
//Redis Client Setname 命令用于指定当前连接的名称。
func (c *Command) SetName (n string)(error){
	if c.conn == nil {
		return RedisConnError
	}
	_,err := c.conn.WriteStringFormat("CLIENT SETNAME %s\r\n",n)
	if err != nil {
		return err
	}
	b,err := c.conn.ReadAsString()
	if err != nil {
		return err
	}
	if b != "OK" {
		return RedisError("Unexpected response to CLIENT SETNAME:" + b)
	}
	return nil
}
//Redis Client Pause 命令用于阻塞客户端命令一段时间（以毫秒计）。
func (c *Command) Pause(t time.Duration) (error) {
	if c.conn == nil {
		return RedisConnError
	}
	s := t.Nanoseconds()/1000000

	fmt.Println(s)
	_,err := c.conn.WriteStringFormat("CLIENT PAUSE %d\r\n",s)
	if err != nil {
		return err
	}
	b,err := c.conn.ReadAsString()
	if err != nil {
		return err
	}
	if b != "OK" {
		return RedisError("Unexpected response to CLIENT PAUSE:" + b)
	}
	return nil
}
//Redis Client Slots 命令用于当前的集群状态，以数组形式展示。
func (c *Command) Slots() (string,error){
	if c.conn == nil {
		return "",RedisConnError
	}
	_,err := c.conn.WriteString("CLUSTER SLOTS\r\n")
	if err != nil {
		return "",err
	}

	b,err := c.conn.ReadAsString()
	if err != nil {
		return "",err
	}
	return b,nil
}
//Redis Command 命令用于返回所有的Redis命令的详细信息，以数组形式展示。
func (c *Command) Command()([]string,error) {
	if c.conn == nil {
		return nil,RedisConnError
	}
	_,err := c.conn.WriteString("COMMAND\r\n")
	if err != nil {
		return nil,err
	}

	b,err := c.conn.ReadAsArray()
	if err != nil {
		return nil,err
	}
	//TODO 需要详细解析返回的数据格式
	return b,nil
}
//Redis Command Count 命令用于统计 redis 命令的个数。
func (c *Command) CommandCount() (int64,error) {
	if c.conn == nil {
		return 0,RedisConnError
	}
	_,err := c.conn.WriteString("COMMAND COUNT\r\n")
	if err != nil {
		return 0,err
	}

	b,err := c.conn.ReadAsInt()
	if err != nil {
		return 0,err
	}
	return b,nil
}
//Redis Command Getkeys 命令用于获取所有 key。
func (c *Command) GetKeys(command string,keys ...string) ([]string,error) {
	if c.conn == nil {
		return nil,RedisConnError
	}
	_,err := c.conn.WriteStringFormat("COMMAND GETKEYS %s %s\r\n",command,strings.Join(keys," "))
	if err != nil {
		return nil,err
	}

	b,err := c.conn.ReadAsArray()
	if err != nil {
		return nil,err
	}
	return b,err
}
//Redis Time 命令用于返回当前服务器时间。
func (c *Command) Time() (string,error)  {
	if c.conn == nil {
		return "",RedisConnError
	}
	_,err := c.conn.WriteString("TIME\r\n")
	if err != nil {
		return "",err
	}

	b,err := c.conn.ReadAsArray()
	if err != nil {
		return "",err
	}
	if len(b) == 2{
		return b[0] + " " + b[1],nil
	}
	return "",errors.New("Unexpected response to TIME")
}























